package com.ship.contract.controller.cnooc;

import com.ship.common.core.base.BaseController;
import com.ship.contract.bean.CnoocAuth;
import com.ship.contract.service.CnoocAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author tangshixuan
 * @date 2023-9-13
 */
@RestController
@RequestMapping("/cnoocAuth")
public class CnoocAuthController extends BaseController {

    @Autowired
    private CnoocAuthService cnoocAuthService;
    @PostMapping("/SchemaService")
    public Map<String,Object> schemaService(HttpServletRequest request){
        Map<String,Object> para = (Map<String, Object>) request.getSession().getAttribute("apiRequestPara");
        System.out.println("解密后的结果: " + para);

        Map<String, Object> response = new HashMap<>();
        String bimRequestId = (String) para.get("bimRequestId");
        response.put("bimRequestId", bimRequestId);

        List<Map<String, Object>> account = new ArrayList<>();
        Map<String, Object> attr1 = new HashMap<>();
        attr1.put("multivalued", false);
        attr1.put("name", "name");
        attr1.put("required", true);
        attr1.put("type", "String");

        Map<String, Object> attr2 = new HashMap<>();
        attr2.put("multivalued", false);
        attr2.put("name", "mobile");
        attr2.put("required", true);
        attr2.put("type", "String");

        Map<String, Object> attr3 = new HashMap<>();
        attr3.put("multivalued", false);
        attr3.put("name", "mail_box");
        attr3.put("required", true);
        attr3.put("type", "String");

        Map<String, Object> attr4 = new HashMap<>();
        attr4.put("multivalued", false);
        attr4.put("name", "department");
        attr4.put("required", false);
        attr4.put("type", "String");

        account.add(attr1);
        account.add(attr2);
        account.add(attr3);
        account.add(attr4);

        response.put("account", account);

        return response;
    }
    @PostMapping("/UserCreateService")
    public Map<String,Object> UserCreateService(HttpServletRequest request){
        Map<String,Object> para = (Map<String, Object>) request.getSession().getAttribute("apiRequestPara");
        CnoocAuth cnoocAuth = new CnoocAuth();
        cnoocAuth.setName((String) para.get("name"));
        cnoocAuth.setMobile((String) para.get("mobile"));
        cnoocAuth.setMailBox((String) para.get("mailBox"));
        cnoocAuth.setEnable(1);
        Map<String, Object> response = new HashMap<>();
        try {
            cnoocAuthService.save(cnoocAuth);
            String generatedId = cnoocAuth.getId();
            response.put("bimRequestId", cnoocAuth.getBimRequestId());
            response.put("uid", generatedId);
            response.put("resultCode", "0");
            response.put("message", "success");
        }catch (Exception e){
            response.put("bimRequestId", cnoocAuth.getBimRequestId());
            response.put("resultCode", "500");
            response.put("message", "账号创建失败，报错信息自定义");
        }
        return response;
    }
    @PostMapping("/UserUpdateService")
    public Map<String,Object> UserUpdateService(HttpServletRequest request){
        Map<String, Object> para = (Map<String, Object>) request.getSession().getAttribute("apiRequestPara");
        Map<String, Object> response = new HashMap<>();
        response.put("bimRequestId", para.get("bimRequestId"));
        try {
            CnoocAuth cnoocAuth = cnoocAuthService.getById((Serializable) para.get("bimUid"));
            System.out.println(cnoocAuth);
            if (para.get("name") != null) {
                cnoocAuth.setName((String) para.get("name"));
            }
            if (para.get("") != null) {
                cnoocAuth.setMobile((String) para.get("mobile"));
            }
            if (para.get("mailBox") != null) {
                cnoocAuth.setMailBox((String) para.get("mailBox"));
            }

            if (para.get("__ENABLE__") != null) {
                if (para.get("__ENABLE__").equals("1")) {
                    cnoocAuth.setEnable(1);
                }
//                else if (para.get("__ENABLE__").equals("0"))
                else{
                    cnoocAuth.setEnable(0);
                }
            }
            System.out.println(cnoocAuth);
            cnoocAuthService.updateById(cnoocAuth);
            response.put("resultCode", "0");
            response.put("message", "success");
        }catch (Exception e){
            System.out.println(000000);
            response.put("resultCode", "0");
            response.put("message", "success");
        }
        return response;
    }

}
